package com.efesco.system;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.efesco.common.Tools;

/**
 * employeeBizRight_dataRightDatasetListener
 */
public class employeeBizRight_dataRightDatasetListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
  	String superDeptId=Tools.toStr(dataset.parameters().getString("super_dept_id"));
  	String employeeId=Tools.toStr(dataset.parameters().getString("employee_id"));
  	String foreignEnterpriseId=Tools.toStr(dataset.parameters().getString("foreign_enterprise_id"));
  	String sql="";
  	if (!superDeptId.equals("") &&  !superDeptId.equals("null"))  {
  		sql="select a.isChecked,a.dept_id,a.dept_name " +
			"from " + 
			"(select distinct 1 isChecked,df.dept_id,df.dept_name from s_fe_emp_scope scp,s_fe_dept_employee de,s_dept_frn df " + 
			"where scp.fe_empid2=de.employee_id and de.dept_id=df.dept_id " +
			"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df.deleted=0 and df.foreign_enterprise_id="+foreignEnterpriseId+
			" and scp.fe_empid1<>scp.fe_empid2 and df.super_dept_id =" + superDeptId +
			" union " +
			"select 0 isChecked,df.dept_id,df.dept_name " + 
			"from s_dept_frn df " +
			"where df.deleted=0 and df.foreign_enterprise_id="+foreignEnterpriseId+" and df.super_dept_id=" + superDeptId +"  and df.dept_id not in (" + 
			"select distinct df.dept_id from s_fe_emp_scope scp,s_fe_dept_employee de,s_dept_frn df " +
			"where scp.fe_empid2=de.employee_id and de.dept_id=df.dept_id " +
			"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df.deleted=0 and df.foreign_enterprise_id="+foreignEnterpriseId+
			" and scp.fe_empid1<>scp.fe_empid2 and df.super_dept_id=" + superDeptId +")) a  order by a.dept_id" ;
  	}
  	else  {
  		sql="select a.isChecked,a.dept_id,a.dept_name " +
			"from " + 
			"(select distinct 1 isChecked,df.dept_id,df.dept_name from s_fe_emp_scope scp,s_fe_dept_employee de,s_dept_frn df " + 
			"where scp.fe_empid2=de.employee_id and de.dept_id=df.dept_id " +
			"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df.deleted=0 and df.foreign_enterprise_id="+foreignEnterpriseId+
			" and scp.fe_empid1<>scp.fe_empid2 and df.super_dept_id is null " +
			" union " +
			"select 0 isChecked,df.dept_id,df.dept_name " + 
			"from s_dept_frn df " +
			"where df.deleted=0 and df.foreign_enterprise_id="+foreignEnterpriseId+" and df.super_dept_id is null  and df.dept_id not in (" + 
			"select distinct df.dept_id from s_fe_emp_scope scp,s_fe_dept_employee de,s_dept_frn df " +
			"where scp.fe_empid2=de.employee_id and de.dept_id=df.dept_id " +
			"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df.deleted=0 and df.foreign_enterprise_id="+foreignEnterpriseId+
			" and scp.fe_empid1<>scp.fe_empid2 and df.super_dept_id is null )) a  order by a.dept_id" ;
  	}
  	System.out.println(sql);
	SqlDataset sqlDataset=(SqlDataset)dataset;
	sqlDataset.setSql(sql);
    return true;
  }

}
